package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.orign.SearchOrignBo;
import com.zhuss.bean.vo.core.orign.ListOrignVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.OrignDao;
import com.zhuss.core.db.entity.Orign;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;


/**
 * 查询orign processor
 * @Author: zhushanshuo
 * @Date: 2020/02/20 21:55
 */
@Component("orignRead")
public class OrignRead {
	
	@Autowired
	private OrignDao orignDao;
	
	
	/**
	 * 查询最大的key值
	 * @param 
	 * @return
	 */
	public Integer findMaxKeyIncr(String type) {
		List<Orign> result = orignDao.listAll();
		Collections.sort(result, new Comparator<Orign>() {

			@Override
			public int compare(Orign o1, Orign o2) {
				return o2.getOrignKey() - o1.getOrignKey();
			}
		});
		Integer orignKey = 0;
		if (result.size() > 0) {
			orignKey = result.get(0).getOrignKey();
		}
		return orignKey + 10;
	}
	
	
	/**
	 * 查询最大的sort
	 * @param  
	 * @param 
	 * @return
	 */
	public Integer getMaxSort(String type) {
		List<Orign> list = orignDao.listAll();
		Integer sort = 0;
		for (Orign c : list) {
			Integer s = c.getSort();
			if (s > sort) {
				sort = s;
			}
		}
		return sort;
	}
	
	
	public Orign getBySort(String type, Integer sort) {
		Orign result = null;
		List<Orign> list = orignDao.listAll();
		for (Orign c : list) {
			if (c.getSort().equals(sort)) {
				result = c;
			}
		}
		return result;
	}
	
	
	/**
	 * 根据sort查询orign
	 * @param  
	 * @param 
	 * @return
	 */
	public List<Orign> listBySort(String type, Integer sort) {
		List<Orign> result = new ArrayList<Orign>();
		List<Orign> list = orignDao.listAll();
		for (Orign c : list) {
			if (c.getSort().equals(sort)) {
				result.add(c);
			}
		}
		return result;
	}
	
	/**
	 * 根据sort查询orign
	 * @param  
	 * @param 
	 * @return
	 */
	public List<Orign> listNotIdBySort(String type, Integer sort, Long orignId) {
		List<Orign> result = new ArrayList<>();
		List<Orign> list = orignDao.listAll();
		for (Orign c : list) {
			if (c.getSort().equals(sort) && !c.getOrignId().equals(orignId)) {
				result.add(c);
			}
		}
		return result;
	}
	
	
	/**
	 * orignID列表
	 * @return
	 */
	public Object listOrignKey(String type) {
		List<Orign> orignList = orignDao.listAll();
		List<Long> result = new ArrayList<>();
		for (Orign orign : orignList) {
			result.add(orign.getOrignId());
		}
		return result;
	}
	
	/**
	 * orign
	 * @return
	 */
	public Object listOrign(String type) {
		List<Orign> orignList = orignDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(orignList, result);
		return result;
	}
	
	/**
	 * orign 
	 * @return
	 */
	public Object listOrignId(String type) {
		List<Orign> orignList = orignDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(orignList, result);
		return result;
	}
	
	private void transferListId(List<Orign> orignList, List<Long> result) {
		for (Orign item : orignList) {
			result.add(item.getOrignId());
		}
	}

	private void transferList(List<Orign> orignList, List<ListVo> result) {
		for (Orign item : orignList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getOrignKey().longValue());
			vo.setLabel(item.getValue());
			vo.setTag(item.getTag());
			result.add(vo);
		}
		
	}

	/**
	 * orign列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchOrignBo bo) {
//		bo.setPageSize(Integer.MAX_VALUE);
		PageVo<ListOrignVo> result = new PageVo<ListOrignVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(transfer(orignDao.list(bo)));
        result.setTotal(orignDao.countList(bo));
		return result;
	}
	
	private List<ListOrignVo> transfer(List<ListOrignVo> list) {
		for (ListOrignVo item : list) {
		}
		return list;
	}


	/**
	 * orign详情
	 * @param orignId
	 * @return
	 * @throws AabException
	 */
	public Object detail(Long orignId) throws AabException {
		Object result = orignDao.detail(orignId);
		if (result == null) {
			throw new AabException(ExceptionCode.数据不存在);
		}
		return result;
	}


}
